package com.invest.task.jsl;

import com.invest.common.config.JslConfig;
import com.invest.common.constant.BusinessConstants;
import com.invest.common.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @projectName: invest
 * @author: yuch
 * @description: TODO 获取集思录登录cookie
 * @date: 2025/1/14 17:07
 */


@Component
@Slf4j
public class GetJslCookie {

    @Autowired
    private JslConfig jslConfig;

    public String getCookie() throws IOException {
        log.info("开始获取集思录登录cookies");
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            // 创建HttpPost请求
            HttpPost request = new HttpPost(jslConfig.getLoginUrl());
            request.setHeader("Accept", "application/json, text/javascript, */*; q=0.01");
            request.setHeader("Accept-Encoding", "gzip, deflate, br, zstd");
            request.setHeader("Accept-Language", "zh-CN,zh;q=0.9");
            request.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
            request.setHeader("Origin", jslConfig.getOrigin());
            request.setHeader("Priority", "u=1, i");
            request.setHeader("Referer", jslConfig.getReturnUrl());
            request.setHeader("Sec-Ch-Ua", "\"Google Chrome\";v=\"131\", \"Chromium\";v=\"131\", \"Not_A Brand\";v=\"24\"");
            request.setHeader("Sec-Ch-Ua-Mobile", "?0");
            request.setHeader("Sec-Ch-Ua-Platform", "\"Windows\"");
            request.setHeader("Sec-Fetch-Dest", "empty");
            request.setHeader("Sec-Fetch-Mode", "cors");
            request.setHeader("Sec-Fetch-Site", "same-origin");
            request.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36");
            request.setHeader("X-Requested-With", "XMLHttpRequest");
            // 设置请求体
            StringEntity params = new StringEntity("return_url=https%3A%2F%2Fwww.jisilu.cn%2F&" + "user_name=778c501c45a77bf8b7a28850690c293d&" + "password=f343f087960b997e56376edaf3db2ddf&" + "auto_login=1&" + "aes=1", "UTF-8");
            request.setEntity(params);
            // 发送请求
            try (CloseableHttpResponse response = httpClient.execute(request)) {
                // 获取响应状态码
                int statusCode = response.getStatusLine().getStatusCode();
                if(statusCode != BusinessConstants.HTTP_SUCESS_CODE){
                    throw new ServiceException("获取集思录登录cookies失败！");
                }
                log.info("Status Code: " + statusCode);
                Header[] headers = response.getHeaders("Set-Cookie");
                for (Header header : headers) {
                    if (header.getValue().length() > BusinessConstants.COOKIES_LENGTH) {
                        String value = header.getValue();
                        return value.substring(0, value.indexOf(";"));
                    }
                }

            }
        } catch (ServiceException | IOException e) {
            log.error(e.getMessage());
            throw e;
        }
        return null;
    }


}
